package com.rttmall.shopbackend.app.gambling.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import com.alibaba.fastjson.JSON;
import com.rttmall.shopbackend.app.gambling.pojo.Category;
import com.rttmall.shopbackend.app.gambling.pojo.Product;
import com.rttmall.shopbackend.app.gambling.pojo.ProductCustom;
import com.rttmall.shopbackend.app.gambling.service.ProductService;
import com.rttmall.shopbackend.app.service.UploadImageService;
import com.rttmall.shopbackend.enums.CountryCode;
import com.rttmall.shopbackend.enums.ProductPostType;
import com.rttmall.shopbackend.enums.ProductStatus;
import com.rttmall.shopbackend.enums.ProductType;
import com.rttmall.shopbackend.pojo.Pagination;

@Controller
@RequestMapping("product")
public class ProductController {
	@Value("${product.imgPath}")
	private String imgPath;
	@Autowired
	private ProductService productService;
	@Autowired
	private UploadImageService uploadImageService;

	@RequestMapping(method = RequestMethod.GET)
	public String findProduct() {
		return "app/gambling/ProductList";
	}

	@RequestMapping(value = "addProductPage")
	public String addProductPage() {
		return "app/gambling/ProductAdd";
	}

	@RequestMapping(value = "/queryProduct")
	public @ResponseBody Map queryProduct(
			@RequestParam(defaultValue = "") String json, Pagination grid) {
		ProductCustom productCustom = JSON.parseObject(json,
				ProductCustom.class);
		if (null == productCustom) {
			productCustom = new ProductCustom();
		}
		String productNameOrId = productCustom.getProductNameOrId();
		if (!StringUtils.isEmpty(productNameOrId)) {
			boolean flag = org.apache.commons.lang.StringUtils
					.isNumeric(productNameOrId.trim());
			if (flag) {
				productCustom.setId(Integer.parseInt(productNameOrId.trim()));
			} else {
				productCustom.setNameEn(productNameOrId.trim());
			}
		}
		grid.setStartIndex(grid.getPageIndex() * grid.getPageSize());
		productCustom.setPagination(grid);
		Map map = productService.queryProduct(productCustom);
		return map;
	}
	@RequestMapping(value = "/queryVirtualProduct")
	public @ResponseBody Map queryVirtualProduct(String keyText, Pagination grid) {
		Map map = productService.queryVirtualProduct(keyText,grid);
		return map;
	}

	@RequestMapping("/addProductSubmit")
	public @ResponseBody void addProductSubmit(HttpServletRequest request)
			throws Exception {
    	Product product = new Product();
    	String  nameZh= request.getParameter("nameZh");
    	String  nameEn= request.getParameter("nameEn");
    	String  subtitleZh= request.getParameter("subtitleZh");
    	String  subtitleEn= request.getParameter("subtitleEn");
    	BigDecimal  originalPrice= new BigDecimal(request.getParameter("originalPrice"));
    	Integer  sellingPrice= Integer.parseInt(request.getParameter("sellingPrice"));
    	Integer  stepCount= Integer.parseInt(request.getParameter("stepCount"));
    	Integer  minCount= Integer.parseInt(request.getParameter("minCount"));
    	Integer  productType= Integer.parseInt(request.getParameter("productType"));
    	Integer  postType= Integer.parseInt(request.getParameter("postType"));
    	Integer  status= Integer.parseInt(request.getParameter("status"));
    	String  countryCode= request.getParameter("countryCode");
    	String  productDetailEn= request.getParameter("productDetailEn");
    	String  productDetailZh= request.getParameter("productDetailZh");
    	Integer  categoryId= Integer.parseInt(request.getParameter("categoryId"));
    	product.setNameEn(nameEn);
    	product.setNameZh(nameZh);
    	product.setSubtitleEn(subtitleEn);
    	product.setSubtitleZh(subtitleZh);
    	product.setOriginalPrice(originalPrice);
    	product.setSellingPrice(sellingPrice);
    	product.setStepCount(stepCount);
    	product.setMinCount(minCount);
    	product.setProductType(productType);
    	product.setPostType(postType);
    	product.setStatus(status);
    	product.setCountryCode(countryCode);
    	product.setProductDetailEn(productDetailEn);
    	product.setProductDetailZh(productDetailZh);
    	product.setCategoryId(categoryId);
    	
    	CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
    	MultipartHttpServletRequest multiRequest = null ;
    	if(multipartResolver.isMultipart(request)){
    		multiRequest = (MultipartHttpServletRequest)request;
    	}
    	productService.addProductSubmit(multiRequest,product);
	}
	@RequestMapping("/updateProductSubmit")
	public @ResponseBody void updateProductSubmit(HttpServletRequest request)
			throws Exception {
    	Product product = new Product();
    	Integer  id= Integer.parseInt(request.getParameter("id"));
    	String  nameZh= request.getParameter("nameZh");
    	String  nameEn= request.getParameter("nameEn");
    	String  subtitleZh= request.getParameter("subtitleZh");
    	String  subtitleEn= request.getParameter("subtitleEn");
    	BigDecimal  originalPrice= new BigDecimal(request.getParameter("originalPrice"));
    	Integer  sellingPrice= Integer.parseInt(request.getParameter("sellingPrice"));
    	Integer  stepCount= Integer.parseInt(request.getParameter("stepCount"));
    	Integer  minCount= Integer.parseInt(request.getParameter("minCount"));
    	Integer  productType= Integer.parseInt(request.getParameter("productType"));
    	Integer  postType= Integer.parseInt(request.getParameter("postType"));
    	Integer  status= Integer.parseInt(request.getParameter("status"));
    	String  countryCode= request.getParameter("countryCode");
    	String  productDetailEn= request.getParameter("productDetailEn");
    	String  productDetailZh= request.getParameter("productDetailZh");
    	product.setId(id);
    	product.setNameEn(nameEn);
    	product.setNameZh(nameZh);
    	product.setSubtitleEn(subtitleEn);
    	product.setSubtitleZh(subtitleZh);
    	product.setOriginalPrice(originalPrice);
    	product.setSellingPrice(sellingPrice);
    	product.setStepCount(stepCount);
    	product.setMinCount(minCount);
    	product.setProductType(productType);
    	product.setPostType(postType);
    	product.setStatus(status);
    	product.setCountryCode(countryCode);
    	product.setProductDetailEn(productDetailEn);
    	product.setProductDetailZh(productDetailZh);
    	
    	
    	CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
    	MultipartHttpServletRequest multiRequest = null ;
    	if(multipartResolver.isMultipart(request)){
    		multiRequest = (MultipartHttpServletRequest)request;
    	}
    	productService.updateProductSubmit(multiRequest,product);
	}
	@RequestMapping(value = "/editQueryProduct")
    public
    @ResponseBody
    Product editQueryProduct(@RequestParam Integer id) {
        return productService.editQueryProduct(id);
    }
	@RequestMapping(value = "/kindEditorUpload", method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> kindEditorUpload(
			HttpServletRequest request, HttpServletResponse response) {
		String filePath = "";
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		try {
			filePath = uploadImageService
					.kindEditorUploadImage(multipartRequest);
		} catch (Exception e) {
			e.printStackTrace();
			responseData(null, "上传失败!", 1);
		}
		return responseData(imgPath + filePath, null, 0);
	}

	private Map<String, Object> responseData(String url, String message,
			Integer code) {
		Map<String, Object> msg = new HashMap<String, Object>();
		msg.put("error", code);
		if (StringUtils.isEmpty(url)) {
			msg.put("message", message);
		} else {
			msg.put("url", url);
		}
		return msg;
	}
	
	@RequestMapping(value = "/batchEditProductStatus")
    public
    @ResponseBody
    void batchEditProductStatus(String ids,Integer flag) throws Exception {
    	List<String> productIdList = Arrays.asList(ids.split(","));
    	productService.batchEditProductStatus(productIdList,flag);
    }
	
	@RequestMapping("/getProductStatus")
	public @ResponseBody List getProductStatus() {
		List list = new ArrayList<>();
		for (ProductStatus e : ProductStatus.values()) {
			list.add(e);
		}
		return list;
	}

	@RequestMapping("/getProductPostType")
	public @ResponseBody List getProductPostType() {
		List list = new ArrayList<>();
		for (ProductPostType e : ProductPostType.values()) {
			list.add(e);
		}
		return list;
	}

	@RequestMapping("/getProductType")
	public @ResponseBody List getProductType() {
		List list = new ArrayList<>();
		for (ProductType e : ProductType.values()) {
			list.add(e);
		}
		return list;
	}

	@RequestMapping("/getCountryCode")
	public @ResponseBody List getCountryCode() {
		List list = new ArrayList<>();
		for (CountryCode e : CountryCode.values()) {
			list.add(e);
		}
		return list;
	}
	@RequestMapping(value = "/findAllCategory")
    public
    @ResponseBody
    List<Category> findAllCategory() {
        return productService.findAllCategory();
    }
}
